/*
 * createPCB.c
 *
 *  Created on: Oct 2, 2013
 *      Author: Joseph DeLong
 */

#include <stdlib.h>

/*
 * CREATE PCB
 * Written by Joseph DeLong
 * Debugged by Alex Newcomer
 * Calls allocate_PCB(), setup_PCB(), and inserts the PCB into the proper queue.
 * Parameters: Process Name, Class, Priority
 * Error Checking:
 * 	Name must be unique and valid
 * 	Class must be valid
 * 	Priority must be valid
 */
void createPCB(char processName[9], int processClass, int priority, struct MPX_Q *r, struct MPX_Q *b) {
	struct MPX_PCB *new;
	int gtg = 0;
	if(findPCB(processName) != NULL) { //if name is good & unique
		gtg++;
		if(processClass == 0 || processClass == 1) { //if good process class
			gtg++;
			if(priority >= -128 && priority < 128) { //if good priority
				new = setupPCB(processName, processClass, priority); //do setup for this PCB
				gtg++;
			}
			else { //bad priority
				//send("Bad priority for PCB" + processName + " : " + priority + "\n");
			}
		}
		else { //bad process class
			//send("Bad process class for PCB" + processName + " : " + processClass + "\n");
		}
	}
	else { //bad process name
		//send("Bad process name for PCB: " + processName +". A PCB of this name may already exist.\n");
	}
	if(gtg < 3) { //if PCB creation didn't fully complete, print error message
		//send("Cannot create PCB with name " + processName + ", class " + processClass + ", and priority " + processPriority + ".\n");
	}
	else { //PCB was set up correctly & we're GOOD TO GO!
		insertPCB(new);//insert the newly created PCB into the appropriate queue!
	}
}
